package utilfuncs_test

import (
	"fmt"
	"git.sr.ht/~mil/mobroute/src/utilfuncs"
	"math"
	"testing"
)

func Test_DistHaversine_Zero(t *testing.T) {
	var (
		coordA         = utilfuncs.Coord{Lat: 2, Lon: 2}
		coordB         = utilfuncs.Coord{Lat: 2, Lon: 2}
		expect float64 = 0
	)
	if err := floatsEqual(expect, utilfuncs.DistHaversine(coordA, coordB)); err != nil {
		t.Fatalf("%v", err)
	}
}

func Test_DistHaversine_Case(t *testing.T) {
	var (
		coordA         = utilfuncs.Coord{Lat: 1, Lon: 2}
		coordB         = utilfuncs.Coord{Lat: 2, Lon: 2}
		expect float64 = 111.19
	)
	if err := floatsEqual(expect, utilfuncs.DistHaversine(coordA, coordB)); err != nil {
		t.Fatalf("%v", err)
	}
}

func floatsEqual(a, b float64) error {
	threshold := 0.005
	if diff := math.Abs(a - b); diff < threshold {
		return nil
	} else {
		return fmt.Errorf("Comparison of a=%f b=%f exceeds threshold=%f", a, b, threshold)
	}
}
